#ifndef ASTNODE_H
#define ASTNODE_H

#include <vector>
#include <string>
#include <memory>
#include <iostream>

class ASTNode
{
public:
	std::string type;
	std::string value;
	std::vector<std::unique_ptr<ASTNode>> children;
	int scopeId = -1; // 新增：用于作用域遮蔽的唯一作用域ID
	int tempDataOffset = 0; // 新增：用于记录临时数据offset

	ASTNode(const std::string &t) : type(t) {}
	ASTNode(const std::string &t, const std::string &v) : type(t), value(v) {}
	ASTNode(const std::string &t, const char *v) : type(t), value(v) {}
	
	// 添加拷貝構造函數
	ASTNode(const ASTNode& other) : type(other.type), value(other.value), scopeId(other.scopeId), tempDataOffset(other.tempDataOffset) {
		for (const auto& child : other.children) {
			children.push_back(std::unique_ptr<ASTNode>(new ASTNode(*child)));
		}
	}
	
	// 添加賦值運算符
	ASTNode& operator=(const ASTNode& other) {
		if (this != &other) {
			type = other.type;
			value = other.value;
			scopeId = other.scopeId;
			tempDataOffset = other.tempDataOffset;
			children.clear();
			for (const auto& child : other.children) {
				children.push_back(std::unique_ptr<ASTNode>(new ASTNode(*child)));
			}
		}
		return *this;
	}

	// 不再需要手动析构，unique_ptr会自动管理
	~ASTNode() = default;

	void addChild(ASTNode *child)
	{
		children.emplace_back(child);
	}

	/* 用于测试而添加的部分，打印输出*/
	void print(int depth = 0) const
	{
		for (int i = 0; i < depth; ++i)
			std::cout << "  ";
		std::cout << type;
		if (!value.empty())
			std::cout << ": " << value;
		std::cout << std::endl;

		for (const auto &child : children)
		{
			child->print(depth + 1);
		}
	}
};

#endif